﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using XWord.Fundamental.Domain;
using Iesi.Collections.Generic;
using NHibernateUnitOfWork;
namespace ConsoleApplication1
{
   public class ImportReadTest
    {
       public static void Test1()
       {
           TextReader reader = new StreamReader("D:\\test.txt", Encoding.Default);

           IList<RCArticle> list = UnitOfWork.CurrentSession.CreateCriteria<RCArticle>()
                .List<RCArticle>().Where
                (e => e.CreateTime.Year == 2007 && e.CreateTime.Month == 12).ToList();
           RCArticle art = list.First(e => e.Part == 1);

           AddQuestion(art, reader);
           RCArticle art2 = list.First(e => e.Part == 2);

           AddQuestion(art2, reader);
           UnitOfWork.CurrentSession.Flush();
       }

       public static void Test2()
       {
           TextReader reader = new StreamReader("D:\\test.txt", Encoding.Default);

           IList<RCArticle> list = UnitOfWork.CurrentSession.CreateCriteria<RCArticle>()
                .List<RCArticle>().Where
                (e => e.CreateTime.Year == 2009 && e.CreateTime.Month == 12 && e.ArticleType==2).ToList();
           RCArticle art = list[0];
          
           
           AddClozeQuestion(art, reader);
        UnitOfWork.CurrentSession.Flush();
       }

       public static void AddClozeQuestion(RCArticle art, TextReader reader)
       {
           for (int i = 0; i < 20; i++)
           {
               Question qustion = new Question();
               //string qustionStr = reader.ReadLine().Trim();
               //qustionStr = qustionStr.Substring(qustionStr.IndexOf(".") + 1).Trim();
               qustion.Sentence = string.Empty;
               qustion.Priority = i;
               qustion.Choices = new HashedSet<Choice>();
               //for (int j = 0; j < 4; j++)
               //{
               //    Choice choice = new Choice();
               //    choice.Priority = j;
               //    string choiceStr = reader.ReadLine().Trim();
               //    if (!string.IsNullOrEmpty(choiceStr))
               //    {
               //        choiceStr = choiceStr.Substring(choiceStr.IndexOf(")") + 1).Trim();
               //        choice.Sentence = choiceStr;
               //        qustion.Choices.Add(choice);
               //    }
               //}
               string[] qustionStr = reader.ReadLine().Trim().Replace("\t", "  ").Split(new char[] { ')' });
              // qustionStr = qustionStr.Substring(qustionStr.IndexOf(".") + 1).Trim();
               for (int j = 0; j < 4; j++)
               {
                   Choice choice = new Choice();
                   choice.Priority = j;
                   string choiceStr = qustionStr[j + 1].Trim();
                   if (!string.IsNullOrEmpty(choiceStr))
                   {
                       if(j<3)
                       choiceStr = choiceStr.Substring(0,choiceStr.IndexOf(" ")).Trim();
                       choice.Sentence = choiceStr;
                       qustion.Choices.Add(choice);
                   }
               }

              
               art.Questions.Add(qustion);
           }
       }

       public static void AddQuestion(RCArticle art, TextReader reader)
       {
           for (int i = 0; i < 5; i++)
           {
               Question qustion = new Question();
               string qustionStr = reader.ReadLine().Trim();
               qustionStr = qustionStr.Substring(qustionStr.IndexOf(".") + 1).Trim();
               qustion.Sentence = qustionStr;
               qustion.Priority = i;
               qustion.Choices = new HashedSet<Choice>();
               for (int j = 0; j < 4; j++)
               {
                   Choice choice = new Choice();
                   choice.Priority = j;
                   string choiceStr = reader.ReadLine().Trim();
                   if (!string.IsNullOrEmpty(choiceStr))
                   {
                       choiceStr = choiceStr.Substring(choiceStr.IndexOf(")") + 1).Trim();
                       choice.Sentence = choiceStr;
                       qustion.Choices.Add(choice);
                   }
               }
               art.Questions.Add(qustion);
           }
       }
    }
}
